"""
Case Type   : dsscmd交互式
Case Name   : 交互式模式下，创建目录&文件&查看文件&复制文件&删除文件
Create At   : 2024/10
Owner       : haomeng
Description :
    1、新建目录，查看目录
    2、创建文件，查看文件
    3、重命名文件名
    4、显示带有可选参数的文件的磁盘使用情况
    5、按文件名查找文件所在路径
    6、查看文件信息，文件大小单位指定为KB，显示最小已逻辑写0长度
    7、查看文件信息，文件大小单位指定为KB，不显示最小已逻辑写0长度
    8、将源文件复制到目标文件(同磁阵路径)
    9、将源文件复制到目标文件(磁阵-->物理路径)
    10、将源文件复制到目标文件,路径为相对路径
    11、截断卷组文件
    12、删除卷组文件
    13、删除目录及其内容
    14、清理环境
Expect      :
    1、成功,Succeed to make dir
    2、成功，Succeed to create file
    3、成功，Succeed to rename file
    4、成功，Succeed to du file info
    5、成功，返回文件路径
    6、成功
    7、成功
    8、成功，Succeed to copy file
    9、成功，Succeed to copy file
    10、失败
    11、成功，Success to truncate file
    12、成功，Succeed to remove device
    13、成功，Succeed to rm dir
    14、清理环境成功
History     :
"""


import os
import unittest

from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DsscmdInteractive(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.node = Node('PrimaryDbUser')
        self.directory = 'dsscmd_file'
        self.file = 'file.txt'
        self.new_file1 = 'new_file1.txt'
        self.new_file2 = 'new_file2.txt'
        self.new_dir = os.path.dirname(macro.DB_INSTANCE_PATH)

    def test_dsscmd(self):

        self.log.info('-----获取数据文件夹vg_name-----')
        data_name = self.node.sh(f"source {macro.DB_ENV_PATH};"
                                 f"dsscmd lsvg -m G | awk '{{{{print$1}}}}' | awk 'NR==2'").result()
        self.log.info(data_name)
        self.assertNotIn(' ', data_name.strip(), '未获取到数据文件夹的vg_name')

        text = '-----step1:进入交互式模式,新建目录,查看目录   expect:成功,Succeed to make dir-----'
        self.log.info(text)

        cmd = f'''dsscmd -i'''
        execute_cmd1 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "mkdir -p +{data_name} -d {self.directory}\\n"
                     expect "dsscmd>"
                     send "ls -p +{data_name}/{self.directory}\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd1)
        res1 = self.node.sh(execute_cmd1).result()
        self.log.info(res1)
        self.assertIn('Succeed to make dir', res1, '新建目录失败')
        self.assertIn('Succeed to ls dir info', res1, '查看目录失败')

        text = '-----step2:进入交互式模式,创建文件,查看文件   expect:成功,Succeed to create file-----'
        self.log.info(text)

        execute_cmd2 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "touch -p +{data_name}/{self.directory}/{self.file}\\n"
                     expect "dsscmd>"
                     send "ls -p +{data_name}/{self.directory}/{self.file}\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd2)
        res2 = self.node.sh(execute_cmd2).result()
        self.log.info(res2)
        self.assertIn('Succeed to create file', res2, '新建文件失败')
        self.assertIn('Succeed to ls file info', res2, '查看文件失败')

        text = '-----step3:进入交互式模式,重命名文件名   expect:成功,Succeed to rename file-----'
        self.log.info(text)

        execute_cmd3 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "rename -o +{data_name}/{self.directory}/{self.file} -n +{data_name}/{self.directory}/{self.new_file1}\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd3)
        res3 = self.node.sh(execute_cmd3).result()
        self.log.info(res3)
        self.assertIn('Succeed to rename file', res3, '重命名文件失败')

        text = '-----step4:进入交互式模式,查看带有可选参数的文件的磁盘使用情况  expect:成功,Succeed to du file info-----'
        self.log.info(text)

        execute_cmd4 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "du -p +{data_name}/{self.directory}/{self.new_file1} -f KaS\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd4)
        res4 = self.node.sh(execute_cmd4).result()
        self.log.info(res4)
        self.assertIn('Succeed to du file info', res4, '查看文件的磁盘使用情况失败')

        text = '-----step5:进入交互式模式,按文件名查找文件所在路径  expect:成功,返回文件路径-----'
        self.log.info(text)

        execute_cmd5 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "find -p +{data_name} -n {self.new_file1}\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd5)
        res5 = self.node.sh(execute_cmd5).result()
        self.log.info(res5)
        self.assertIn(f'{data_name}/{self.directory}/{self.new_file1}', res5, '查看文件路径失败')

        text = '-----step6:进入交互式模式,查看文件信息,文件大小单位指定为KB,显示最小已逻辑写0长度  expect:成功-----'
        self.log.info(text)

        execute_cmd6 = f'''source {macro.DB_ENV_PATH};
                    expect <<EOF
                    set timeout 3
                    spawn {cmd}
                    expect "dsscmd>"
                    send "ls -p +{data_name}/{self.directory}/{self.new_file1} -m K -w 1\\n"
                    expect "dsscmd>"
                    send "exit\\n"
                    expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd6)
        res6 = self.node.sh(execute_cmd6).result()
        self.log.info(res6)
        self.assertIn('min_inited_size', res6.splitlines()[2], '逻辑写0不存在')
        self.assertIn('Succeed to ls file info', res6, '查看文件信息失败')

        text = '-----step7:进入交互式模式,查看文件信息,不显示最小已连续逻辑写0长度  expect:成功-----'
        self.log.info(text)

        execute_cmd7 = f'''source {macro.DB_ENV_PATH};
                    expect <<EOF
                    set timeout 3
                    spawn {cmd}
                    expect "dsscmd>"
                    send "ls -p +{data_name}/{self.directory}/{self.new_file1} -m K -w 0\\n"
                    expect "dsscmd>"
                    send "exit\\n"
                    expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd7)
        res7 = self.node.sh(execute_cmd7).result()
        self.log.info(res7)
        self.assertNotIn('min_inited_size', res7.splitlines()[2], '逻辑写0存在')
        self.assertIn('Succeed to ls file info', res7, '查看文件信息失败')

        text = '-----step8:进入交互式模式,将源文件复制到目标文件(同磁阵路径)  expect:成功-----'
        self.log.info(text)

        execute_cmd8 = f'''source {macro.DB_ENV_PATH};
                   expect <<EOF
                   set timeout 3
                   spawn {cmd}
                   expect "dsscmd>"
                   send "cp -s +{data_name}/{self.directory}/{self.new_file1} -d +{data_name}/{self.directory}/{self.new_file2}\\n"
                   expect "dsscmd>"
                   send "exit\\n"
                   expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd8)
        res8 = self.node.sh(execute_cmd8).result()
        self.log.info(res8)
        self.assertIn('Succeed to copy file', res8, '复制文件失败')

        text = '-----step9:进入交互式模式,将源文件复制到目标文件(磁阵->物理路径)  expect:成功-----'
        self.log.info(text)

        execute_cmd9 = f'''source {macro.DB_ENV_PATH};
                   expect <<EOF
                   set timeout 3
                   spawn {cmd}
                   expect "dsscmd>"
                   send "cp -s +{data_name}/{self.directory}/{self.new_file1} -d {self.new_dir}/{self.new_file2}\\n"
                   expect "dsscmd>"
                   send "exit\\n"
                   expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd9)
        res9 = self.node.sh(execute_cmd9).result()
        self.log.info(res9)
        self.assertIn('Succeed to copy file', res9, '复制文件失败')

        text = '-----step10:进入交互式模式,将源文件复制到目标文件,路径为相对路径  expect:失败-----'
        self.log.info(text)

        execute_cmd10 = f'''source {macro.DB_ENV_PATH};
                   expect <<EOF
                   set timeout 3
                   spawn {cmd}
                   expect "dsscmd>"
                   send "cp -s +{data_name}/{self.directory}/{self.new_file1} -d ../{self.directory}/{self.new_file2}\\n"
                   expect "dsscmd>"
                   send "exit\\n"
                   expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd10)
        res10 = self.node.sh(execute_cmd10).result()
        self.log.info(res10)
        self.assertIn('Failed to copy file', res10, '复制文件成功')

        text = '-----step11:进入交互式模式,截断卷组文件  expect:成功,success to truncate file-----'
        self.log.info(text)

        execute_cmd11 = f'''source {macro.DB_ENV_PATH};
                   expect <<EOF
                   set timeout 3
                   spawn {cmd}
                   expect "dsscmd>"
                   send "truncate -p +{data_name}/{self.directory}/{self.new_file1} -l 1\\n"
                   expect "dsscmd>"
                   send "exit\\n"
                   expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd11)
        res11 = self.node.sh(execute_cmd11).result()
        self.log.info(res11)
        self.assertIn('Success to truncate file', res11, '截断卷组文件失败')

        text = '-----step12:进入交互式模式,删除卷组文件  expect:成功,success to remove device-----'
        self.log.info(text)

        execute_cmd12 = f'''source {macro.DB_ENV_PATH};
                   expect <<EOF
                   set timeout 3
                   spawn {cmd}
                   expect "dsscmd>"
                   send "rm -p +{data_name}/{self.directory}/{self.new_file1}\\n"
                   expect "dsscmd>"
                   send "exit\\n"
                   expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd12)
        res12 = self.node.sh(execute_cmd12).result()
        self.log.info(res12)
        self.assertIn('Succeed to remove device', res12, '删除卷组文件失败')

        text = '-----step13:进入交互式模式,删除目录及其内容  expect:成功,success to rm dir-----'
        self.log.info(text)

        execute_cmd13 = f'''source {macro.DB_ENV_PATH};
                   expect <<EOF
                   set timeout 3
                   spawn {cmd}
                   expect "dsscmd>"
                   send "rmdir -p +{data_name}/{self.directory} -r\\n"
                   expect "dsscmd>"
                   send "exit\\n"
                   expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd13)
        res13 = self.node.sh(execute_cmd13).result()
        self.log.info(res13)
        self.assertIn('Succeed to rm dir', res13, '删除目录失败')

    def tearDown(self):
        text = '-----step14:清理环境  expect:清理环境成功-----'
        self.log.info(text)
        clear_cmd = f'rm -rf {self.new_dir}/{self.new_file2}'
        self.log.info(clear_cmd)
        clear_res = self.node.sh(clear_cmd).result()
        self.log.info(clear_res)
        self.assertIn('', clear_res, '清理环境失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
